package lib.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import lib.Base.Education;
import lib.Base.Info;
import lib.Base.Message;
import lib.Base.Post;
import lib.Base.Profile;
import lib.Base.Workplace;
import lib.Com.Container;
import lib.Common.Path;
import lib.FileSystem.Check;
import lib.XML.XMLParser;
import lib.XML.XMLWriter;

import org.swixml.SwingEngine;

public class GuiProfile extends JPanel {
	/**
	 * Class which represents the profile page
	 */
	protected Profile profileData;
	protected Info profileInfo;
	
	protected List<Education> educationItems;
	protected List<Workplace> workplaceItems;
	protected List<Message> guestbookItems;
	protected List<Post> posts;
	
	protected List<GuiProfileEducation> educationWindows;
	protected List<GuiProfileWorkplace> workplaceWindows;
	protected List<GuiProfileGuestbook> guestbookWindows;
	protected List<GuiProfilePost> postsWindows;
	
	protected JPanel root;
	protected JPanel editProfile;
	protected JLabel picture;
	protected JTextField username;
	protected JTextField firstname;
	protected JTextField lastname;
	protected JTextField birthday;
	protected JTextArea about;
	
	protected Box educationVBox;
	protected Box workplaceVBox;
	protected Box guestbookVBox;
	protected Box postVBox;
	
	private SwingEngine engine;
	
	public GuiProfile(String userId, boolean ownProfile){
		
		try{
			engine = new SwingEngine(this);
			engine.render(new File(Path.GUI_PATH + "gui-profile.xml"));
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		XMLParser parser = null;

		try {

			if(Check.EXISTS(Path.XML_PATH_PROFILES + userId+".xml")){
				parser = new XMLParser();
				profileData = parser.ParseProfile(Path.XML_PATH_PROFILES + userId + ".xml");
			}else{
				profileData = new Profile();
			}

			if(profileData.getInfo() == null) {
				profileInfo = new Info();
				profileData.setInfo(profileInfo);
			}else {
				profileInfo = profileData.getInfo();
			}
			
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	parser = null;
        }
        
        if(ownProfile){
			JButton btn = new JButton();
			btn.setName("Edit");
			btn.setText("Edit");
			btn.setActionCommand("Edit");
			
			btn.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					
					if(e.getActionCommand().equals("Edit")){
						editProf();
					}
					
				}
				
			});
			
			editProfile.add(btn);
		}
        
        this.educationWindows = new ArrayList<GuiProfileEducation>();
        this.workplaceWindows = new ArrayList<GuiProfileWorkplace>();
        this.postsWindows = new ArrayList<GuiProfilePost>();
        this.guestbookWindows = new ArrayList<GuiProfileGuestbook>();
        
        fillProfile();
        fillPost();
        fillGuestbook();
        fillEducation();
        fillWorkplace();
        
        this.setVisible(true);
	}
	
	protected void editProf(){
		GuiProfileEdit tmpEdit = null;
		
		try{
			tmpEdit = new GuiProfileEdit(profileData, root.getLocation());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		fillProfile();
	}
	
	protected JPanel getPanel(){
		return root;
	}
	
	protected void fillPost(){
		
		if(postsWindows.size() == 0){
			for(Post p: posts){
				postsWindows.add(new GuiProfilePost(p));
			}
			
			for(GuiProfilePost p: postsWindows){
				postVBox.add(p.getPanel());
			}
		}else{
			postVBox.removeAll();
			
			for(Post p: posts){
				postsWindows.add(new GuiProfilePost(p));
			}
			
			for(GuiProfilePost p: postsWindows){
				postVBox.add(p.getPanel());
			}
		}
		
		for(Post p: posts){
			postsWindows.add(new GuiProfilePost(p));
		}
		
		for(GuiProfilePost p: postsWindows){
			postVBox.add(p.getPanel());
		}
	}
	
	protected void fillGuestbook(){
		for(Message msg: guestbookItems){
			guestbookWindows.add(new GuiProfileGuestbook(msg));
		}
		
		for(GuiProfileGuestbook gpg: guestbookWindows){
			guestbookVBox.add(gpg.getPanel());
		}
	}
	
	protected void fillEducation(){
		for(Education e: educationItems){
			educationWindows.add(new GuiProfileEducation(e));
		}
		
		for(GuiProfileEducation gpe: educationWindows){
			educationVBox.add(gpe.getPanel());
		}
	}
	
	protected void fillWorkplace(){
		for(Workplace w: workplaceItems){
			workplaceWindows.add(new GuiProfileWorkplace(w));
		}
		
		for(GuiProfileWorkplace gpw: workplaceWindows){
			workplaceVBox.add(gpw.getPanel());
		}
	}
	
	protected void fillProfile(){
		this.posts = profileData.getPosts();
		this.educationItems = profileInfo.getEducations();
		this.workplaceItems = profileInfo.getWorkplaces();
		this.guestbookItems = profileData.getGuestbook();
			
		this.picture.setText(profileData.getPictureId());
		this.username.setText(profileData.getUsername());
		this.firstname.setText(profileInfo.getFirstName());
		this.lastname.setText(profileInfo.getLastName());
		this.birthday.setText(profileInfo.getGebDay() + "." + profileInfo.getGebMont() + "." + profileInfo.getGebYear());
		this.about.setText(profileInfo.getAbout());
		root.repaint();
	}
	
	protected void fill(){
		fillPost();
	}
}